package com.lanou.tipsy.fragment.person;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lanou.tipsy.R;
import com.lanou.tipsy.adapter.collect.MyCollectAdapter;
import com.lanou.tipsy.base.BaseFragment;
import com.lanou.tipsy.db.TipsyEntity;
import com.lanou.tipsy.db.TipsyEntityDao;
import com.lanou.tipsy.tools.volley.DaoSingleton;
import com.umeng.socialize.utils.Log;

import java.util.List;

/**
 * Created by wangshuai on 15/11/6.
 * 我的收藏界面
 */
public class MyCollectFragment extends BaseFragment {

    private TextView tv_collect_list;//标题
    private ListView lv_collect_list;//列表
    private MyCollectAdapter adapter;//声明适配器
    private ImageView iv_collect_list_back;//声明返回按钮
    private DaoSingleton daoSingleton;
    private TipsyEntityDao tipsyEntityDao;
    private TipsyEntity tipsyEntity;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fr_collect_music_lists, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initview(getView());
    }

    private void initview(View view) {
        tv_collect_list = (TextView) view.findViewById(R.id.tv_collect_list);
        tv_collect_list.setText("收藏界面");
        lv_collect_list = (ListView) view.findViewById(R.id.lv_collect_list);
        iv_collect_list_back = (ImageView) view.findViewById(R.id.iv_collect_list_back);
        iv_collect_list_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                getActivity().getSupportFragmentManager().popBackStack();
            }
        });

        //初始化适配器
        adapter = new MyCollectAdapter(getActivity());
        //绑定适配器
        lv_collect_list.setAdapter(adapter);
        //列表长按取消收藏监听
        lv_collect_list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {

                //初始化
                tipsyEntity = new TipsyEntity();
                daoSingleton = DaoSingleton.getInstance();
                tipsyEntityDao = DaoSingleton.getInstance().getTipsyEntityDao();
                tipsyEntity = (TipsyEntity) parent.getItemAtPosition(position);
                //添加dialog自定义属性
                new AlertDialog.Builder(getContext())
                        .setTitle("确定删除" + tipsyEntity.getSongTitle() + "?????")
                        .setIcon(R.mipmap.tipsy_icon)
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                int p = position + 1;
                                Toast.makeText(getContext(), "您选择了第" + p + "个 : "
                                        + tipsyEntity.getSongTitle(), Toast.LENGTH_SHORT).show();
                                String songtitle = tipsyEntity.getSongTitle();
                                List<TipsyEntity> deleteQuery = daoSingleton.getTipsyEntityDao().queryBuilder()
                                        .where(TipsyEntityDao.Properties.SongTitle.eq(songtitle)).list();
                                tipsyEntityDao.delete(deleteQuery.get(0));
                                Toast.makeText(getActivity(), "歌曲取消收藏", Toast.LENGTH_SHORT).show();
                                adapter = new MyCollectAdapter(getActivity());
                                //绑定适配器
                                lv_collect_list.setAdapter(adapter);
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        }).show();
                return false;
            }
        });


    }

    //进行页面刷新
    @Override
    public void onResume() {
        super.onResume();
        Log.i("test", " MyCollectFragment  >>>>>>> + 页面刷新.....");
        //刷新页面数据,从新加载适配器,更新数据
        //初始化适配器
        adapter = new MyCollectAdapter(getActivity());
        //绑定适配器
        lv_collect_list.setAdapter(adapter);


    }


}

